<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wx:wxMemberUser:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wx:wxMemberUser:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wx:wxMemberUser:remove']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wx:wxMemberUser:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="wxMemberUserList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="ID" align="center" prop="id" /> -->
      <el-table-column label="手机号" align="center" prop="phone" :show-overflow-tooltip="true"/>
      <el-table-column label="性别" align="center" prop="sex" :formatter="sfktFormate"/>
      <el-table-column label="昵称" align="center" prop="name" :show-overflow-tooltip="true"/>
      <el-table-column label="头像" align="center" prop="avator"> <template slot-scope="scope"> <el-image :src="scope.row.avator" style="width: 80px" :preview-src-list="[scope.row.avator]"/> </template> </el-table-column>
      <el-table-column label="注册时间" align="center" prop="createAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wx:wxMemberUser:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wx:wxMemberUser:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户,会员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" maxlength="10"/>
        </el-form-item>
        <!-- <el-form-item label="性别">
          <el-radio-group v-model="form.sex">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-form-item label="邀请码" prop="code">
          <el-input v-model="form.code" placeholder="请输入邀请码" />
        </el-form-item> -->
        <el-form-item label="昵称" prop="name">
          <el-input v-model="form.name" placeholder="请输入昵称" maxlength="15"/>
        </el-form-item>
        <el-form-item label="头像">
          <imageUpload v-model="form.avator"/>
        </el-form-item>
        <!-- <el-form-item label="推荐人" prop="introUser">
          <el-input v-model="form.introUser" placeholder="请输入推荐人" />
        </el-form-item>
        <el-form-item label="间接上级" prop="indirectUser">
          <el-input v-model="form.indirectUser" placeholder="请输入间接上级" />
        </el-form-item>
        <el-form-item label="0普通用户1商家2企业用户" prop="level">
          <el-input v-model="form.level" placeholder="请输入0普通用户1商家2企业用户" />
        </el-form-item>
        <el-form-item label="可用余额" prop="money">
          <el-input v-model="form.money" placeholder="请输入可用余额" />
        </el-form-item>
        <el-form-item label="冻结金额" prop="freeze">
          <el-input v-model="form.freeze" placeholder="请输入冻结金额" />
        </el-form-item>
        <el-form-item label="极光推送id" prop="auroraId">
          <el-input v-model="form.auroraId" placeholder="请输入极光推送id" />
        </el-form-item>
        <el-form-item label="注册时间" prop="createAt">
          <el-date-picker clearable size="small"
            v-model="form.createAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择注册时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updateAt">
          <el-date-picker clearable size="small"
            v-model="form.updateAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择更新时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="微信openId" prop="wechatOpenId">
          <el-input v-model="form.wechatOpenId" placeholder="请输入微信openId" />
        </el-form-item>
        <el-form-item label="是否冻结账号(0-正常,1-冻结)" prop="disabled">
          <el-input v-model="form.disabled" placeholder="请输入是否冻结账号(0-正常,1-冻结)" />
        </el-form-item>
        <el-form-item label="删除标记" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记" />
        </el-form-item>
        <el-form-item label="二维码地址" prop="qrcode">
          <el-input v-model="form.qrcode" placeholder="请输入二维码地址" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listWxMemberUser, getWxMemberUser, delWxMemberUser, addWxMemberUser, updateWxMemberUser, exportWxMemberUser } from "@/api/wx/wxMemberUser";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "WxMemberUser",
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户,会员表格数据
      wxMemberUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: null,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        phone: [
          { required: true, message: "手机号不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户,会员列表 */
    getList() {
      this.loading = true;
      listWxMemberUser(this.queryParams).then(response => {
        this.wxMemberUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        phone: null,
        password: null,
        sex: 0,
        code: null,
        name: null,
        avator: null,
        introUser: null,
        indirectUser: null,
        level: null,
        money: null,
        freeze: null,
        auroraId: null,
        createAt: null,
        updateAt: null,
        wechatOpenId: null,
        disabled: null,
        delFlag: null,
        qrcode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //table表格格式化
    sfktFormate(row,index){
      if (row.sex == 0) {
        return "男";
      } else if(row.sex == 1){
        return "女";
      }else{
        return "未知";
      }
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户,会员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWxMemberUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户,会员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWxMemberUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWxMemberUser(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户,会员选中数据', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWxMemberUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户,会员数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportWxMemberUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
